<?php
class ProductShowAction extends AgriculturalproductsCommonAction {

	
	public function index() {		
		//查询购物车数量
	   	
		
		$this->leftCommonQuery();
		
		if(empty($_REQUEST ['pro_type_id'])){
			$this->showProductType();			
		}else{
			$singleCondition ;
			$orderField = '' ;
			//没有选择分类直接进行价格和销量的排序
			if (($_REQUEST ['pro_type_id']=='pro_catalog_id' && empty($_REQUEST ['pro_catalog_id']))){
				$singleCondition['status'] = 1 ;
				if ($_REQUEST['orderColumn'] == 'price' && $_REQUEST['orderType'] == 'asc'){
					$orderField = 'local_price asc' ;	
				}
				if ($_REQUEST['orderColumn'] == 'price' && $_REQUEST['orderType'] == 'desc'){
					$orderField = 'local_price desc' ;	
				}
				if ($_REQUEST['orderColumn'] == 'salesamount' && $_REQUEST['orderType'] == 'asc'){
					$orderField = 'salesamount asc' ;	
				}
				if ($_REQUEST['orderColumn'] == 'salesamount' && $_REQUEST['orderType'] == 'desc'){
					$orderField = 'salesamount desc' ;	
				}
				$this->getAllAndPartList() ;
			}
			//选择了分类后再进行价格和销量的排序
			else{
				$queryString_part = '' ;
			if (empty($_REQUEST ['pro_catalog_id'])){
				$queryString_part = "select concat(c.name,' ( ',count(1),' ) ') as departproduct ,
								a.pro_type_id as pro_type_id ,
								a.pro_catalog_id as pro_catalog_id
								from product a , product_catalog  c 
								where 
								a.pro_type_id = " .$_REQUEST ['pro_type_id'] . "								
								and a.pro_catalog_id = c.id
								group by c.name ";		
			}else{
					$queryString_part = "select concat(c.name,' ( ',count(1),' ) ') as departproduct ,
									a.pro_type_id as pro_type_id ,
									a.pro_catalog_id as pro_catalog_id
									from product a , product_catalog  c 
									where 
									a.pro_type_id = " .$_REQUEST ['pro_type_id'] . "
									and a.pro_catalog_id = ".$_REQUEST ['pro_catalog_id'] ."
									and a.pro_catalog_id = c.id
									group by c.name ";		
				}
				
				$this->assign("partList",$this->CommonSimplequery(1,$queryString_part ,'' ,'' ,0,""));
				
				//产品展示后的类型名，根据pro_type_id进行查询
				$queryString_productShow = "select * from product_type where id = "  . $_REQUEST ['pro_type_id']  . " and status = 1";
				
				$this->assign("productShowVo",$this->CommonSimplequery(1,$queryString_productShow ,'' ,'' ,0,""));		
							
				$singleCondition['status'] = 1 ;
				$singleCondition['pro_type_id'] = $_REQUEST ['pro_type_id'] ;
				if(!empty($_REQUEST ['pro_catalog_id'])){
					$singleCondition['pro_catalog_id'] = $_REQUEST ['pro_catalog_id'] ;
				}
				
				$orderField = '' ;
				
			
				if ( $_REQUEST['orderType'] == 'desc' && $_REQUEST['orderColumn']=='price' ){
					$orderField = ' local_price desc' ;
				}else if ($_REQUEST['orderType'] == 'asc' && $_REQUEST['orderColumn']=='price'){
					$orderField = 'local_price asc' ;
				}else if ( $_REQUEST['orderType'] == 'desc' && $_REQUEST['orderColumn']=='salesamount' ) {
					$orderField = ' salesamount desc' ;
				}else if ( $_REQUEST['orderType'] == 'asc' && $_REQUEST['orderColumn']=='salesamount' ) {
					$orderField = ' salesamount asc' ;
				}else{
					$orderField = 'statusdate DESC' ;	
				}
				
				$this->assign('pro_catalog_id',$_REQUEST ['pro_catalog_id']);
				$this->assign('pro_type_id',$_REQUEST ['pro_type_id']);
			}
			
			
			
			
			$this->getQueryPage('product',$singleCondition,$orderField);	
		}
		
		$this->display();
	}
	
	
	private function leftCommonQuery(){
		//最新上架
		$this->getlastUpdateList();		
	}
	
	private function getAllAndPartList(){
		$queryString_all = "select concat('全部',' ( ',count(1),' ) ') as allproduct from viewproduct										where status = 1 ";			
		$this->assign("allList",$this->CommonSimplequery(1,$queryString_all ,'' ,'' ,0,""));
		//部分分类		
		$queryString_part = "select  case when b.id <> '' then concat(a.`name`,' ( ',count(1),' ) ') 	else concat(a.`name`,' ( ',0,' ) ') end as departproduct ,
							b.pro_type_id as pro_type_id,
							a.name as pro_type_name
							from product_type a  left join product b 
							on b.pro_type_id = a.id
							where a.`status` = 1 
							group by a.`name`
							order by b.statusdate desc ";		
		$this->assign("partList",$this->CommonSimplequery(1,$queryString_part ,'' ,'' ,0,""));
	}
	
	
	private function showProductType(){
		
		$this->getAllAndPartList() ;
		
		$singleCondition['status'] = 1 ;
		$orderField = 'statusdate DESC' ;
		$this->getQueryPage('product',$singleCondition,$orderField);
	}
	
	
	/**
	 * 
	 * 首页查询产品
	 */
	public function search(){
		
		$this->leftCommonQuery();
		
		$this->showProductType();
		
		$singleCondition['status'] = 1 ;
		$singleCondition['name'] = iconv("gb2312","UTF-8",$_REQUEST ['name']) ;  ;
		$orderField = 'statusdate DESC' ;
		$this->getQueryPage('product',$singleCondition,$orderField);	
		$this->assign('isSearch','search') ;
		$this->display('./Tpl/default/ProductShow/index.html');
	}

}
?>